#include "musicdao.h"

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>

musicDao::musicDao(QObject *parent) : QObject(parent)
{

}

//添加音乐函数
void musicDao::addMusic(Music *mus)
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName(R"(D:\Workroom\Mine Qt\VehicleMutimedia\SQLite\music.db)");
    db.open();

    //先查找数据库确认没有相同数据再添加
    QSqlQuery querySearch;
    QString sqlSearch=QString("select * from t_music where name='%1'").arg(mus->name);
    querySearch.exec(sqlSearch);

    int count=0;
    while(querySearch.next())
    {
        count++;
    }

    if(count==0)  //没有相同数据
    {
        QSqlQuery query;
        QString sql = QString("insert into t_music(name,pathMusic,pathLRC) values('%1','%2','%3');")
                .arg(mus->name).arg(mus->pathMusic).arg(mus->pathLrc);
        query.exec(sql);
    }
    else qDebug()<<"存在歌曲："<<mus->name<<endl;

    db.close();
}
